#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define READ 0
#define WRITE 1
#define BIGINT 128
#define NUMPARENT 2 //assuming 2 parent processes
#define FILESPERC 5 //assuming 5 files per child

void* doWork(void* arg);
pthread_t workerA, workerB;
void *resultA, *resultB;
int fdA[2], fdB[2];
int status;

int main(int argc, char* argv[])
{
	//gets the number of this parent
	//int numsFromT[FILESPERC+FILESPERC];
	int numFromParent;
	read(STDOUT_FILENO, &numFromParent, sizeof(BIGINT)); 
	numFromParent = 4;
	
	write (STDOUT_FILENO, (const void *)&numFromParent, sizeof(BIGINT));
	
	
	if (pipe (fdA) < 0) {
        perror ("plumbing problem");
        exit(1);
    }
    if (pipe (fdB) < 0) {
        perror ("plumbing problem");
        exit(1);
    }
    
	if ((status = pthread_create(&workerA, NULL, doWork, (void *)numFromParent)) != 0) {
		fprintf(stderr, "join error %d: %s\n", status, strerror(status));
		exit(1);
	}
	numFromParent--;
	if ((status = pthread_create(&workerB, NULL, doWork, (void *)numFromParent)) != 0) {
		fprintf(stderr, "join error %d: %s\n", status, strerror(status));
		exit(1);
	}
	status = pthread_detach(workerA);
	status = pthread_detach(workerB);
	
	int k = 0;
	int wrtA = 1000, rdA = 0;
	int wrtB = 1000, rdB = 0;
	while(k < FILESPERC+FILESPERC){
		if(wrtA == 1000 && rdA < FILESPERC){
			read(fdA[READ], &wrtA, sizeof(BIGINT));
			rdA++;
		}
		if(wrtB == 1000 && rdB < FILESPERC){
			read(fdB[READ], &wrtB, sizeof(BIGINT));
			rdB++;
		}
		
		if(wrtA < wrtB){
			write (STDOUT_FILENO, (const void *)&wrtA, sizeof(BIGINT));
			//numsFromT[k] = wrtA;
			wrtA = 1000;
		} else {
			write (STDOUT_FILENO, (const void *)&wrtB, sizeof(BIGINT));
			//numsFromT[k] = wrtB;
			wrtB = 1000;
		}
		k++;
	}
    
	
	/*
    k = 0;
    while(k < FILESPERC+FILESPERC)
    	printf("Number: %d\n", numsFromT[k++]);
	*/
	
	sleep(8);
    
	return 0;
}

void* doWork(void* arg)
{
	
	//For loading files.
	int gotNum = (int)arg;
	char fileName[64] = "file";
	char fileType[8] = ".txt";
	char fileNumb[8];
	gotNum = sprintf(fileNumb, "%d", gotNum);
	int allNumIn[FILESPERC];
	int allNumOut[FILESPERC];
	int i = 4;	
	int j = 0;
	char* allChar[FILESPERC];
	char fBuf1[128];
	char* ptr;
	
	//Setup file name
	fileName[i++] = fileNumb[0];
    while(fileType[j] != '\0')
      fileName[i++] = fileType[j++];

    fileName[i] = '\0'; 
    //printf("Loading the file name: %s\n", fileName); 
	
    //Get file contents
    i = 0;
	FILE* fp1 = fopen(fileName, "r");
	while((ptr = fgets(fBuf1, 128, fp1) != NULL)) {
		fBuf1[strlen(fBuf1)-1] = 0;
		allChar[i++] = strdup(fBuf1);
	}
	//printf("SUCCESS! We loaded %d lines.\n", i);
	fclose(fp1);
	
	//Converts the char to int
	i = 0;
	while(i < FILESPERC){
		allNumOut[i] = atoi(allChar[i]);
		i++;
	}
	
	//sorts them low to high
	i = FILESPERC - 1;
	j = 0;
	int numTemp = 0;
	int numTempHigh = 0;
	int tempJ = 0;
	while(i >= 0){
		numTempHigh = allNumOut[0];
		tempJ = 0;
		j = 1;
		while(j < FILESPERC){
			numTemp = allNumOut[j];
			if(numTemp > numTempHigh){
				numTempHigh = numTemp;
				tempJ = j;
			}
			j++;
		}
		allNumIn[i] = allNumOut[tempJ];
		allNumOut[tempJ] = 0;
		i--;
	}
	
	//write to the Parent
	i = 0;
	if(((int)arg % 2) == 0){
		while(i < FILESPERC){
			write(fdA[WRITE], (const void *)&allNumIn[i], sizeof(BIGINT));
			i++;
		}
	} else {
		while(i < FILESPERC){
			write(fdB[WRITE], (const void *)&allNumIn[i], sizeof(BIGINT));
			i++;
		}
	}
	return arg;
}